package com.hhit.bigscreenbackmanage.service.impl.tgygl;

import com.alibaba.fastjson.JSONObject;
import com.hhit.bigscreenbackmanage.dao.tgygl.TTgyglProgressComparisonYcMapper;
import com.hhit.bigscreenbackmanage.entity.tgygl.TTgyglProgressComparisonYc;
import com.hhit.bigscreenbackmanage.service.tgygl.TTgyglProgressComparisonYcService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.hhit.bigscreenbackmanage.dao.tgygl.TTgyglProgressComparisonYcMapper;
import com.hhit.bigscreenbackmanage.entity.tgygl.TTgyglProgressComparison;
import com.hhit.bigscreenbackmanage.entity.tgygl.TTgyglProgressComparisonYc;
import com.hhit.bigscreenbackmanage.service.tgygl.TTgyglProgressComparisonYcService;
import org.seimicrawler.xpath.JXNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author zhl
 * @Date 2021/7/22
 */
@Service
public class TTgyglProgressComparisonYcServiceImpl implements TTgyglProgressComparisonYcService {

    private Logger log = LoggerFactory.getLogger(TTgyglProgressComparisonYcServiceImpl.class);

    @Autowired
    TTgyglProgressComparisonYcMapper tTgyglProgressComparisonYcMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return 0;
    }

    @Override
    public int insert(TTgyglProgressComparisonYc record) {
        return 0;
    }

    @Override
    public int insertSelective(TTgyglProgressComparisonYc record) {
        return 0;
    }

    @Override
    public TTgyglProgressComparisonYc selectByPrimaryKey(Integer id) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(TTgyglProgressComparisonYc record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(TTgyglProgressComparisonYc record) {
        return 0;
    }

    @Override
    public Map<String, Object> queryJdwcqkCount(JSONObject jsonObject) throws Exception {
        if (jsonObject.get("dataTime") == null || "".equals(jsonObject.get("dataTime"))) {
            throw new Exception("缺少必要参数: dataTime");
        }
        Map<String, Object> result = new HashMap<>();

        List<Map<String, Object>> data = new ArrayList<>();
        try {
            data = tTgyglProgressComparisonYcMapper.queryJdwcqkCount(jsonObject);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new Exception("数据库内部错误");
        }

        Long wcs = Long.valueOf(0);
        Long zhs = Long.valueOf(0);
        for (Map<String, Object> map : data) {
            wcs = Long.sum(wcs, (Long) map.get("wcs"));
            zhs = Long.sum(zhs, (Long) map.get("zhs"));
        }
        Long zs = Long.sum(wcs, zhs);
        Double wcl = Double.valueOf(0);
        if (zs != 0) {
            wcl = new BigDecimal((float) wcs / zs * 100).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        }

        result.put("wcs", wcs);
        result.put("zhs", zhs);
        result.put("wcl", wcl);
        result.put("list", data);

        return result;
    }

    public void insertWord(JXNode jxNode, int id, int year, int month) throws Exception {

        //先删除对应的数据
        tTgyglProgressComparisonYcMapper.deleteByNfAndFf(year, month);

        List<JXNode> trNodeList = jxNode.sel("//tr");

        TTgyglProgressComparisonYc tTgyglProgressComparisonYc;
        //标包名称
        String packageName = "";

        for (JXNode item : trNodeList) {
            int index = trNodeList.indexOf(item);
            //A表包 在第一行
            if (index == 1) {
                packageName = getPackageName(year, id, month, item);
            }

            //B表包 在第15行
            if (index == 15) {
                packageName = getPackageName(year, id, month, item);
            }
            //C表包 在第22行
            if (index == 22) {
                packageName = getPackageName(year, id, month, item);
            }
            //D表包 在第29行
            if (index == 29) {
                packageName = getPackageName(year, id, month, item);
            }
            //从D表包开始往下
            if (index != 0 && index != 1 && index != 15 && index != 22 && index != 29) {
                List<JXNode> tdNodeList = item.sel("//td");
                tTgyglProgressComparisonYc = new TTgyglProgressComparisonYc();

                //年
                tTgyglProgressComparisonYc.setYear(year);
                //月
                tTgyglProgressComparisonYc.setMonth(month);
                //文件id
                tTgyglProgressComparisonYc.setFileId(id);
                //标包名
                tTgyglProgressComparisonYc.setPackageName(packageName);
                //计划进度
                tTgyglProgressComparisonYc.setPlanProgress(
                        tdNodeList.get(1).selOne("//p//text()") == null ? "" : tdNodeList.get(1).selOne("//p//text()").toString());
                //实际进度
                tTgyglProgressComparisonYc.setActualProgress(
                        tdNodeList.get(2).selOne("//p//text()") == null ? "" : tdNodeList.get(2).selOne("//p//text()").toString());
                //实际进度
                tTgyglProgressComparisonYc.setRemarks(
                        tdNodeList.get(3).selOne("//p//text()") == null ? "" : tdNodeList.get(3).selOne("//p//text()").toString());
                //保存数据
                tTgyglProgressComparisonYcMapper.insert(tTgyglProgressComparisonYc);
            }
        }
    }

    //获取报名并且保存数据
    private String getPackageName(int year, int id, int month, JXNode item) {
        TTgyglProgressComparisonYc tTgyglProgressComparisonYc;
        String packageName;
        List<JXNode> tdNodeList = item.sel("//td");
        tTgyglProgressComparisonYc = new TTgyglProgressComparisonYc();

        //年
        tTgyglProgressComparisonYc.setYear(year);
        //月
        tTgyglProgressComparisonYc.setMonth(month);
        //文件id
        tTgyglProgressComparisonYc.setFileId(id);
        //标包A
        packageName = tdNodeList.get(1).selOne("//p//text()") == null ? "" : tdNodeList.get(1).selOne("//p//text()").toString();
        //标包名
        tTgyglProgressComparisonYc.setPackageName(packageName);
        //计划进度
        tTgyglProgressComparisonYc.setPlanProgress(
                tdNodeList.get(2).selOne("//p//text()") == null ? "" : tdNodeList.get(2).selOne("//p//text()").toString());
        //实际进度
        tTgyglProgressComparisonYc.setActualProgress(
                tdNodeList.get(3).selOne("//p//text()") == null ? "" : tdNodeList.get(3).selOne("//p//text()").toString());
        //实际进度
        tTgyglProgressComparisonYc.setRemarks(
                tdNodeList.get(4).selOne("//p//text()") == null ? "" : tdNodeList.get(4).selOne("//p//text()").toString());
        //保存数据
        tTgyglProgressComparisonYcMapper.insert(tTgyglProgressComparisonYc);
        return packageName;
    }

    @Override
    public List<TTgyglProgressComparisonYc> selectListByFileId(Integer fileId) {
        List<TTgyglProgressComparisonYc> resulList = null;
        TTgyglProgressComparisonYc tTgyglProgressComparisonYc = new TTgyglProgressComparisonYc();
        tTgyglProgressComparisonYc.setFileId(fileId);
        try {
            resulList = tTgyglProgressComparisonYcMapper.selectByBean(tTgyglProgressComparisonYc);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resulList;
    }
}
